package com.source.lightfowl.basedata.service.impl;

import com.source.lightfowl.VO.*;
import com.source.lightfowl.VO.chart.*;
import com.source.lightfowl.basedata.dao.*;
import com.source.lightfowl.basedata.entity.PoultryCategory;
import com.source.lightfowl.basedata.entity.Poultry_Parent;
import com.source.lightfowl.basedata.entity.SlaughterHouse;
import com.source.lightfowl.basedata.service.PoultrySalesChartService;
import com.source.lightfowl.labelingRecord.dao.RecordDao;
import com.source.lightfowl.labelingRecord.pojo.Record;
import com.source.lightfowl.utils.DateUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigInteger;
import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: Faink
 * @CreateTime: 2020/9/11 19:33
 */
@Service
public class PoultrySalesChartServiceImpl implements PoultrySalesChartService {

    @Autowired
    private PoultrySalesDao poultrySalesDao;
    @Autowired
    private RecordDao recordDao;
    @Autowired
    private PoultryCategoryDao poultryCategoryDao;
    @Autowired
    private PoultryParentDao poultryParentDao;
    @Autowired
    private SlaughterHouseDao slaughterHouseDao;
    @Autowired
    private StallsDao stallsDao;
    @Autowired
    private DateUtils dateUtils;

    public List<PoultryChartVO> findAllSalesData(int id) {
        List<PoultryChartVO> result = new ArrayList<>();;
        List<PoultryCategory> poultryCategoryListByHouseId = poultryCategoryDao.findByHouseId(id);
        if (null != poultryCategoryListByHouseId && poultryCategoryListByHouseId.size() !=0) {
            PoultryChartVO weeklist = new PoultryChartVO();//存放周数据
            result.add(weeklist);
            //初始化饼状图数据weekList的属性名和数据
            weeklist.setProperty("week");
            List weekData = recordDao.findAllWeekNum();
            weeklist.setData(weekData);

            //查出禽类种类数量，建立本周内种类销量数组
            BigInteger numOfSpecies = poultryCategoryDao.findNumOfPoultry(id);
            for (int i=0; i<numOfSpecies.intValue(); i++) {
                List data = new ArrayList();
                PoultryChartVO list = new PoultryChartVO(data);
                result.add(list);
            }
            int index = 1;
            List<PoultryCategory> allPoultry = poultryCategoryDao.findByHouseId(id);
            for (PoultryCategory poultry : allPoultry) {
                PoultryChartVO vo = result.get(index);
                vo.setProperty(poultry.getName());
                index++;
            }
            for(Object week : weeklist.getData()) {
                for (PoultryCategory p : allPoultry) {
                    for (int i=1; i<result.size(); i++) {
                        PoultryChartVO vo = result.get(i);
                        if (vo.getProperty().equals(p.getName())) {
                            Integer numOfSale = recordDao.findNumOfSaleByWeekAndId((Integer) week, p.getId());
                            if (numOfSale == null) vo.getData().add(0);
                            else vo.getData().add(numOfSale);
                        }
                    }
                }

            }
        }
        return result;
/*
            //查出按周次、种类分组的销量组
            List<Object> groupByWeekAndPoultryList = recordDao.findGroupByWeekAndPoultry();
            List<RecordCount> countList = new ArrayList<>();
            for (Object o : groupByWeekAndPoultryList) {
                Object[] row = (Object[]) o;
                RecordCount recordCount = new RecordCount();
                recordCount.setWeek((Integer) row[0]);
                recordCount.setPoultryId((String)row[1]);
                recordCount.setSalesSum((BigInteger)row[2]);

                String poultryName = poultryCategoryDao.findNameById((String)row[1]);
                PoultryChartVO vo = result.get(index);
                vo.setProperty(poultryName);
                index++;
            }*/

    }

    @Override
    public List<PoultryChartVO> findParentSalesSort(int id) {
        return null;
    }

    @Override
    public List<PoultrySalesVO> findAllSales(int id) {
        List<Object> objects = poultrySalesDao.findAllSalesByHouse(id);
        List<PoultrySalesVO> list = new ArrayList<>();
        if (null == objects || objects.size() == 0) return list;
        for (Object o : objects) {
            Object[] row = (Object[]) o;
            PoultrySalesVO vo = new PoultrySalesVO();
            vo.setPoultryId((String)row[0]);
            vo.setLabelingDate((Date)row[1]);
            vo.setBarcodeMachineId((String) row[2]);
            vo.setBarcodeNumber((String)row[3]);
            vo.setPoultryName((String)row[4]);
            vo.setSalesTerminalId((BigInteger)row[5]);
            vo.setSalesAddr((String)row[6]);
            vo.setSalesTerminalName((String)row[7]);
            vo.setWeek((Integer)row[8]);
            vo.setSalesSum((BigInteger)row[9]);
            vo.setHouseAddr((String)row[10]);
            vo.setHouseName((String)row[11]);
            vo.setHouseLatitude((String)row[12]);
            vo.setHouseLongitude((String)row[13]);

            list.add(vo);
        }
        return list;
    }

    @Override
    public List<PoultrySalesVO> findAllSalesById(String id) {
        List<Object> objects = poultrySalesDao.findAllSalesById(id);
        List<PoultrySalesVO> list = new ArrayList<>();
        if (null == objects || objects.size() == 0) return list;
        for (Object o : objects) {
            Object[] row = (Object[]) o;
            PoultrySalesVO vo = new PoultrySalesVO();
            vo.setPoultryId((String)row[0]);
            vo.setLabelingDate((Date)row[1]);
            vo.setBarcodeMachineId((String) row[2]);
            vo.setBarcodeNumber((String)row[3]);
            vo.setPoultryName((String)row[4]);
            vo.setSalesTerminalId((BigInteger)row[5]);
            vo.setSalesAddr((String)row[6]);
            vo.setSalesTerminalName((String)row[7]);
            vo.setWeek((Integer)row[8]);
            vo.setSalesSum((BigInteger)row[9]);
            vo.setHouseAddr((String)row[10]);
            vo.setHouseName((String)row[11]);
            vo.setHouseLatitude((String)row[12]);
            vo.setHouseLongitude((String)row[13]);

            list.add(vo);
        }
        return list;
    }

    @Override
    public List<RecordToVO> findAllRecordTo(int id) {
        List<Object> objects = poultrySalesDao.findAllRecordByBarcodeId(id);
        List<RecordToVO> list = new ArrayList<>();
        if (null == objects || objects.size() == 0) return list;
        for (Object o : objects) {
            Object[] row = (Object[]) o;
            RecordToVO vo = new RecordToVO();
            vo.setBarcodeMachineId((String) row[0]);
            vo.setLabelingDate((Date) row[1]);
            vo.setBarcodeNumber((String) row[2]);
            vo.setPoultryName((String)row[3]);
            vo.setSalesAddr((String)row[4]);
            vo.setSalesTerminalName((String) row[5]);
            list.add(vo);
        }
        return list;
    }

    @Override
    public List<Poultry_Parent> findAllParent() {
        return poultryParentDao.findAll();
    }

    @Override
    public List<PoultryCategory> findPoultryByParentId(String id) {
        return poultryCategoryDao.findByParentId(id);
    }

    @Override
    public Map<String, BigInteger> findCountOfParent(int houseId) {
        Map<String, BigInteger> result = new HashMap<>();
        List<Poultry_Parent> allParent = poultryParentDao.findAll();
        if (null != allParent) {
            for (Poultry_Parent parent : allParent) {
                BigInteger count = poultrySalesDao
                        .findCountByHouseIdAndParentId(houseId + "", parent.getId());
                result.put(parent.getPoultryParentName(), count);
            }
        }
        return result;
    }

    @Override
    public Map<String, BigInteger> findCountOfParentThisYear(int houseId) {
        Map<String, BigInteger> result = new HashMap<>();
        List<Poultry_Parent> allParent = poultryParentDao.findAll();
        BigInteger countOfParent = BigInteger.ZERO;
        if (null != allParent) {
            for (Poultry_Parent parent : allParent) {
                BigInteger count = poultrySalesDao
                        .findCountByHouseIdAndParentIdThisYear(houseId + "", parent.getId());
                result.put(parent.getPoultryParentName(), count);
                countOfParent = countOfParent.add(count);
            }
        }
        result.put("all", countOfParent);
        return result;
    }

    @Override
    public Map<String, BigInteger> findSalesInYears(String parentId, int houseId) {
        Map<String, BigInteger> result = new HashMap<>();
        List<String> idList = poultryCategoryDao
                .findIdByParentAndHouseId(parentId, houseId+"");
        if (null != idList) {
            List<Object> countOfPoultryInYear = recordDao
                    .findCountOfPoultryInYear(idList);
            if (null != countOfPoultryInYear || countOfPoultryInYear.size()>0) {
                for (Object object : countOfPoultryInYear) {
                    Object[] ob = (Object[]) object;
                    result.put((String)ob[1], (BigInteger) ob[0]);
                }
            }
        }
        return result;
    }

    @Override
    public ParentPoultryBarChartVO findParentSalesDataTenDaysOfMonthAgo(int house_id, String pid) {

        List<String> parentNameList = new ArrayList<>();
        //一个月有三个10天
        List<List<BigInteger>> sale_datas = new ArrayList<>();
        /*List<BigInteger> sale_data1 = new ArrayList<>();
        List<BigInteger> sale_data2 = new ArrayList<>();
        List<BigInteger> sale_data3 = new ArrayList<>();
        sale_datas.add(sale_data1);
        sale_datas.add(sale_data2);
        sale_datas.add(sale_data3);*/
        List<String> date = new ArrayList<>();//存10天的间隔日期
        if ("all".equals(pid)) {
            List<Poultry_Parent> parentList = poultryParentDao.findAll();
            if (null != parentList) {
                for (Poultry_Parent parent : parentList) {
                    List<BigInteger> curParentSale = new ArrayList<>();
                    sale_datas.add(curParentSale);
                    String parent_id = parent.getId();
                    parentNameList.add(parent.getPoultryParentName());
                    List<String> poultryList =
                            poultryCategoryDao.findIdByParentAndHouseId(parent_id, house_id + "");
                    if (null != poultryList && poultryList.size()>0) {
                        BigInteger firstTenDays =
                                recordDao.findSalesThirtyDaysAgo1(poultryList);
                        BigInteger secondTenDays =
                                recordDao.findSalesThirtyDaysAgo2(poultryList);
                        BigInteger thirdTenDays =
                                recordDao.findSalesThirtyDaysAgo3(poultryList);
                        curParentSale.add(firstTenDays==null ? BigInteger.ZERO : firstTenDays);
                        curParentSale.add(secondTenDays==null ? BigInteger.ZERO : secondTenDays);
                        curParentSale.add(thirdTenDays==null ? BigInteger.ZERO : thirdTenDays);
                        /*sale_data1.add(firstTenDays==null ? BigInteger.ZERO : firstTenDays);//第一个十天
                        sale_data2.add(secondTenDays==null ? BigInteger.ZERO : secondTenDays);//第二个十天
                        sale_data3.add(thirdTenDays==null ? BigInteger.ZERO : thirdTenDays);//第三个十天*/
                    }/* else {

                        sale_data1.add(BigInteger.ZERO);//第一个十天
                        sale_data2.add(BigInteger.ZERO);//第二个十天
                        sale_data3.add(BigInteger.ZERO);//第三个十天
                    }*/

                }

            } else return null;
        } else {
            List<String> poultryList =
                    poultryCategoryDao.findIdByParentAndHouseId(pid, house_id + "");
            List<BigInteger> curParentSale = new ArrayList<>();
            sale_datas.add(curParentSale);
            if (null != poultryList && poultryList.size()>0) {
                BigInteger firstTenDays =
                        recordDao.findSalesThirtyDaysAgo1(poultryList);
                BigInteger secondTenDays =
                        recordDao.findSalesThirtyDaysAgo2(poultryList);
                BigInteger thirdTenDays =
                        recordDao.findSalesThirtyDaysAgo3(poultryList);
                curParentSale.add(firstTenDays==null ? BigInteger.ZERO : firstTenDays);
                curParentSale.add(secondTenDays==null ? BigInteger.ZERO : secondTenDays);
                curParentSale.add(thirdTenDays==null ? BigInteger.ZERO : thirdTenDays);
                /*sale_data1.add(firstTenDays==null ? BigInteger.ZERO : firstTenDays);//第一个十天
                sale_data2.add(secondTenDays==null ? BigInteger.ZERO : secondTenDays);//第二个十天
                sale_data3.add(thirdTenDays==null ? BigInteger.ZERO : thirdTenDays);//第三个十天*/
            }/* else {
                sale_data1.add(BigInteger.ZERO);//第一个十天
                sale_data2.add(BigInteger.ZERO);//第二个十天
                sale_data3.add(BigInteger.ZERO);//第三个十天
            }*/
        }

        //一个月/10日 的 日期边界
        List<Object> tenDaysBorder = recordDao.findTenDaysBorder();
        Object[] objects = (Object[]) tenDaysBorder.get(0);
        String border1 = objects[0] + "~" + objects[1];
        String border2 = objects[2] + "~" + objects[3];
        String border3 = objects[4] + "~" + objects[5];
        date.add(border1);
        date.add(border2);
        date.add(border3);
        ParentPoultryBarChartVO result =
                new ParentPoultryBarChartVO(parentNameList, sale_datas, date);
        return result;
    }

    @Override
    public List<MaxSaleHouseVO> findMaxSalesHouseThreeMonthAgo() {
        List<Poultry_Parent> parentList = poultryParentDao.findAll();
        List<MaxSaleHouseVO> result = new ArrayList<>();
        if (null != parentList) {
            for (Poultry_Parent parent : parentList) {
                MaxSaleHouseVO vo = new MaxSaleHouseVO();
                String parent_id = parent.getId();
                vo.setFirstSaleHouseOfThreeMonthsAgo(
                        poultrySalesDao.findMaxSaleOfParentHouseThreeMonthsAgo(parent_id)
                );
                vo.setFirstSaleHouseOfTwoMonthsAgo(
                        poultrySalesDao.findMaxSaleOfParentHouseTwoMonthsAgo(parent_id)
                );
                vo.setFirstSaleHouseOfOneMonthAgo(
                        poultrySalesDao.findMaxSaleOfParentHouseOneMonthsAgo(parent_id)
                );
                vo.setSpecies(parent.getPoultryParentName());
                result.add(vo);
            }
        }
        return result;
    }

    @Override
    public List<SaleVO> findSaleCountThisYear() {
        List<SaleVO> result = new ArrayList<>();
        List<Poultry_Parent> allParent = poultryParentDao.findAll();
        if (null != allParent) {
            for (Poultry_Parent parent : allParent) {
                String parentId = parent.getId();
                List<String> idsByParent = poultryCategoryDao.findIdByParent(parentId);
                BigInteger countThisYear = recordDao.findCountByParent(idsByParent);
                result.add(new SaleVO(countThisYear, parent.getPoultryParentName()));
            }
        }
        return result;
    }

    @Override
    public List<SaleVO> findSaleCountThisYear(int houseId) {
        List<SaleVO> result = new ArrayList<>();
        List<Poultry_Parent> allParent = poultryParentDao.findAll();
        if (null != allParent) {
            for (Poultry_Parent parent : allParent) {
                String parentId = parent.getId();
                List<String> idsByParent = poultryCategoryDao.findIdByParentAndHouseId(parentId, houseId+"");
                BigInteger countThisYear = BigInteger.ZERO;
                if (null != idsByParent && idsByParent.size()>0) {
                    countThisYear = recordDao.findCountByParentThisYear(idsByParent);
                }
                result.add(new SaleVO(countThisYear, parent.getPoultryParentName()));
            }
        }
        return result;
    }

    @Override
    public List<SaleVO> findSaleCount(int houseId) {
        List<SaleVO> result = new ArrayList<>();
        List<Poultry_Parent> allParent = poultryParentDao.findAll();
        if (null != allParent) {
            for (Poultry_Parent parent : allParent) {
                String parentId = parent.getId();
                List<String> idsByParent = poultryCategoryDao.findIdByParentAndHouseId(parentId, houseId+"");
                BigInteger count = BigInteger.ZERO;
                if (null != idsByParent && idsByParent.size()>0) {
                    count = recordDao.findCountByParent(idsByParent);
                }
                result.add(new SaleVO(count, parent.getPoultryParentName()));
            }
        }
        return result;
    }

    @Override
    public ParentsSaleInMonthOfYearVO findSaleOfParentByHouseInMonth(int houseId) {
        ParentsSaleInMonthOfYearVO result = new ParentsSaleInMonthOfYearVO();
        List<Poultry_Parent> allParent = poultryParentDao.findAll();
        List<String> parents = new ArrayList<>();
        List<Integer> months = new ArrayList<>();
        List<List<BigInteger>> sale_datas = new ArrayList<>();
        int month = LocalDate.now().getMonth().getValue();
        for (int i=1; i<=month; i++) months.add(i);
        if (null != allParent && allParent.size()>0) {
            for (Poultry_Parent parent : allParent) {
                List<BigInteger> sale_data = new ArrayList<>();
                parents.add(parent.getPoultryParentName());
                String parentId = parent.getId();
                List<String> idsByParent =
                        poultryCategoryDao.findIdByParentAndHouseId(parentId, houseId+"");
                if(null != idsByParent && idsByParent.size()>0) {
                    for (int i=1; i<=month; i++) {
                        BigInteger countInMonth =
                                recordDao.findCountByParentInMonth(idsByParent, i);
                        sale_data.add(countInMonth);
                    }
                } else {
                    for (int i=1; i<=month; i++) {
                        sale_data.add(BigInteger.ZERO);
                    }
                }
                sale_datas.add(sale_data);
            }
        } else return null;
        result.setParents(parents);
        result.setMonths(months);
        result.setSales_datas(sale_datas);
        return result;
    }

    @Override
    public List<PoultrySalesTableVO> findBestSaleOfMarketOneMonthsAgo(int houseId, String parentId) {
        List<PoultrySalesTableVO> result = new ArrayList<>();
        if (!"all".equals(parentId)) {
            String name = poultryParentDao.findNameById(parentId);
            List<Object> marketSortBySales = poultrySalesDao.findMarketSortByParentSales(houseId + "", parentId);
            if (null != marketSortBySales) {
                for (Object o : marketSortBySales) {
                    Object[] objects = (Object[]) o;
                    //拼接地址
                    String address = "" + objects[0];
                    address = address + "" + objects[1];
                    address = address + "" + objects[2];
                    address = address + "" + objects[3];
                    result.add(
                            new PoultrySalesTableVO(
                                    name, address, (String) objects[4],
                                    (BigInteger)objects[5]
                            )
                    );
                }
            }
        } else {
            List<Object> marketSortBySales = poultrySalesDao.findMarketSortBySales(houseId + "");
            if (null != marketSortBySales) {
                for (Object o : marketSortBySales) {
                    Object[] objects = (Object[]) o;
                    //拼接地址
                    String address = "" + objects[0];
                    address = address + "" + objects[1];
                    address = address + "" + objects[2];
                    address = address + "" + objects[3];
                    result.add(
                            new PoultrySalesTableVO(
                                    "所有禽类", address,
                                    (String) objects[4], (BigInteger)objects[5]
                            )
                    );
                }
            }
        }
        return result;
    }

    @Override
    public List<HouseRelationVO> findRelationOfHouseInThisYear(int houseId) {
        List<HouseRelationVO> result = new ArrayList<>();
        HouseRelationVO resource_huose = new HouseRelationVO();
        SlaughterHouse house = slaughterHouseDao.findById(houseId).get();
        resource_huose.setHouseName(house.getName());
        resource_huose.setLatitude(house.getLatitude());
        resource_huose.setLongitude(house.getLongitude());
        resource_huose.setSaleCount(poultrySalesDao.findHouseSaleCount(houseId+""));
        result.add(resource_huose);
        List<Object> markets = poultrySalesDao.findSaleByHouseAndMarket(houseId + "");
        Map<String, MarketAndSaleCount> map = new ConcurrentHashMap<>();//去重需要用到，以空间换时间的思路
        if (null != markets && markets.size() >0) {
            for (Object o : markets) {
                Object[] objects = (Object[]) o;
                HouseRelationVO vo = new HouseRelationVO(
                        (String) objects[1],
                        (BigInteger) objects[0],
                        (String) objects[2],
                        (String) objects[3]
                );
                MarketAndSaleCount copy = new MarketAndSaleCount(
                        (String) objects[1],
                        (BigInteger) objects[0]
                );
                //由于可能会销售到去向市场的不同档口，而去向市场与去向档口在同一张表
                //需要去重操作以及销量相加，不修改表结构的情况下使用HashMap写算法去重
                String coordinates = vo.getLongitude() + "," + vo.getLatitude();//经纬度
                BigInteger count = vo.getSaleCount();
                if (map.containsKey(coordinates)) {
                    MarketAndSaleCount cur = map.get(coordinates);
                    BigInteger newCount = cur.getCount().add(count);
                    cur.setCount(newCount);
                    //map.put(coordinates, cur);

                } else map.put(coordinates, copy);

            }
            //遍历去重完成的map，加入结果集
            for (Map.Entry<String, MarketAndSaleCount> entry : map.entrySet()) {
                String key = entry.getKey();
                //分开经纬度
                String[] longitudeAndLatitude = key.split(",");
                MarketAndSaleCount val = entry.getValue();
                HouseRelationVO vo = new HouseRelationVO(
                        val.getName(),
                        val.getCount(),
                        longitudeAndLatitude[0],
                        longitudeAndLatitude[1]
                );
                result.add(vo);
            }

        }
        return result;
    }

    @Override
    public List<SaleVO> findDefaultCount() {
        List<SaleVO> result = new ArrayList<>();
        List<Poultry_Parent> allParent = poultryParentDao.findAll();
        if (null != allParent) {
            for (Poultry_Parent parent : allParent) {
                String parentId = parent.getId();
                List<String> idsByParent = poultryCategoryDao.findIdByParent(parentId);
                BigInteger count = BigInteger.ZERO;
                if (null != idsByParent && idsByParent.size()>0) {
                    count = recordDao.findCountByParentInAMonth(idsByParent);
                }
                result.add(new SaleVO(count, parent.getPoultryParentName()));
            }
        }
        return result;
    }

    @Override
    public List<DateSaleVO> findDefaultCountByDays() {
        //Map<String, List<SaleVO>> result = new ConcurrentHashMap<>();
        List<DateSaleVO> result = new ArrayList<>();
        List<Poultry_Parent> allParent = poultryParentDao.findAll();
        if (null != allParent) {
            for (int numOfMonth=0; numOfMonth<30; numOfMonth++) {
                List<SaleVO> list = new ArrayList<>();
                for (Poultry_Parent parent : allParent) {
                    String parentId = parent.getId();
                    List<String> idsByParent = poultryCategoryDao.findIdByParent(parentId);
                    BigInteger count = BigInteger.ZERO;
                    if (null != idsByParent && idsByParent.size()>0) {
                        count = recordDao.findCountAndDateByParentInAMonth(idsByParent, numOfMonth);
                    }
                    list.add(new SaleVO(count, parent.getPoultryParentName()));
                }
                result.add(new DateSaleVO(dateUtils.getBeforeDaysDate(numOfMonth), list));
            }
        }
        return result;
    }

    @Override
    public List<SaleVO> findCountByNameAndTime(String stallName, String linkName, String startDate, String endDate) {
        List<Poultry_Parent> allParent = poultryParentDao.findAll();
        List<SaleVO> result = new ArrayList<>();
        if (null == allParent || allParent.size() ==0) return result;
        //匹配档口名
        if (!StringUtils.isEmpty(stallName)) {
            //档口名不为空 判断联系人是为空
            //1.根据档口名字或者联系人名字模糊匹配出档口id
            List<Integer> list = stallsDao.findIdByStallName(stallName);
            if (StringUtils.isEmpty(linkName)){ //联系人为空
                for (Poultry_Parent parent : allParent) {
                    String parentId = parent.getId();
                    List<String> idsByParent = poultryCategoryDao.findIdByParent(parentId);
                    //2.查询出这些档口近一个月的条码数
                    BigInteger count = recordDao.findByStallNearTime(list,startDate+" 00:00:00",endDate+" 23:59:59", idsByParent);
                    //3.分类进行封装 封装成前端需要的格式
                    result.add(new SaleVO(count, parent.getPoultryParentName()));
                }
                return result;
            }else { //联系人不为空
                List<Integer> list2 = stallsDao.findIdByStallName(linkName);
                List<Integer> sandId = getSandId(list, list2); //获取交集
                for (Poultry_Parent parent : allParent) {
                    String parentId = parent.getId();
                    List<String> idsByParent = poultryCategoryDao.findIdByParent(parentId);
                    //2.查询出这些档口近一个月的条码数
                    BigInteger count = recordDao.findByStallNearTime(sandId,startDate+" 00:00:00",endDate+" 23:59:59", idsByParent);
                    //3.分类进行封装 封装成前端需要的格式
                    result.add(new SaleVO(count, parent.getPoultryParentName()));
                }
                return result;
            }

        }else {
            //档口名为空
            if (StringUtils.isEmpty(linkName)){ //联系人为空
                for (Poultry_Parent parent : allParent) {
                    String parentId = parent.getId();
                    List<String> idsByParent = poultryCategoryDao.findIdByParent(parentId);
                    //2.查询出这些档口近一个月的条码数
                    BigInteger count = recordDao.findAllNearTime(startDate+" 00:00:00",endDate+" 23:59:59", idsByParent);
                    //3.分类进行封装 封装成前端需要的格式
                    result.add(new SaleVO(count, parent.getPoultryParentName()));
                }
                return result;
            }else { //联系人不为空
                List<Integer> list3 = stallsDao.findIdByUserName(linkName);
                for (Poultry_Parent parent : allParent) {
                    String parentId = parent.getId();
                    List<String> idsByParent = poultryCategoryDao.findIdByParent(parentId);
                    //2.查询出这些档口近一个月的条码数
                    BigInteger count = recordDao.findByStallNearTime(list3,startDate+" 00:00:00",endDate+" 23:59:59", idsByParent);
                    //3.分类进行封装 封装成前端需要的格式
                    result.add(new SaleVO(count, parent.getPoultryParentName()));
                }
                return result;
            }
        }
    }

    @Override
    public List<DateSaleVO> findCountByNameAndTimeByDays(String stallName, String linkName, String startDate, String endDate) {
        //Map<String, List<SaleVO>> resultMap = new ConcurrentHashMap<>();
        List<DateSaleVO> resultMap = new ArrayList<>();
        List<Poultry_Parent> allParent = poultryParentDao.findAll();

        Date date = dateUtils.stringToDate(endDate);
        Date start = dateUtils.stringToDate(startDate);
        Date end = dateUtils.stringToDate(endDate);
        if (null == allParent || allParent.size() ==0) return resultMap;
        //时间条件
        /*boolean condition = (date.after(start) && date.before(end))
                || date.equals(start) || date.equals(end);*/
        //匹配档口名
        if (!StringUtils.isEmpty(stallName)) {
            //档口名不为空 判断联系人是为空
            //1.根据档口名字或者联系人名字模糊匹配出档口id
            List<Integer> list = stallsDao.findIdByStallName(stallName);
            if (StringUtils.isEmpty(linkName)){ //联系人为空
                while ((date.after(start) && date.before(end))
                        || date.equals(start) || date.equals(end)) {
                    List<SaleVO> result = new ArrayList<>();
                    for (Poultry_Parent parent : allParent) {
                        String parentId = parent.getId();
                        List<String> idsByParent = poultryCategoryDao.findIdByParent(parentId);
                        //2.查询出这些档口近一个月的条码数
                        BigInteger count = recordDao.findByStallNearTime(list, idsByParent, date);
                        //3.分类进行封装 封装成前端需要的格式
                        result.add(new SaleVO(count, parent.getPoultryParentName()));
                    }
                    resultMap.add(new DateSaleVO(dateUtils.stringToYMD(date), result));
                    //日期自减1
                    date = dateUtils.getDateBeforeDaysDate(1, date);
                }
                return resultMap;
            }else { //联系人不为空
                List<Integer> list2 = stallsDao.findIdByStallName(linkName);
                List<Integer> sandId = getSandId(list, list2); //获取交集
                while ((date.after(start) && date.before(end))
                        || date.equals(start) || date.equals(end)) {
                    List<SaleVO> result = new ArrayList<>();
                    for (Poultry_Parent parent : allParent) {
                        String parentId = parent.getId();
                        List<String> idsByParent = poultryCategoryDao.findIdByParent(parentId);
                        //2.查询出这些档口近一个月的条码数
                        BigInteger count = recordDao.findByStallNearTime(sandId, idsByParent, date);
                        //3.分类进行封装 封装成前端需要的格式
                        result.add(new SaleVO(count, parent.getPoultryParentName()));
                    }
                    resultMap.add(new DateSaleVO(dateUtils.stringToYMD(date), result));
                    //日期自减1
                    date = dateUtils.getDateBeforeDaysDate(1, date);
                }
                return resultMap;
            }

        }else {
            //档口名为空
            if (StringUtils.isEmpty(linkName)){ //联系人为空
                while ((date.after(start) && date.before(end))
                        || date.equals(start) || date.equals(end)) {
                    List<SaleVO> result = new ArrayList<>();
                    for (Poultry_Parent parent : allParent) {
                        String parentId = parent.getId();
                        List<String> idsByParent = poultryCategoryDao.findIdByParent(parentId);
                        //2.查询出这些档口近一个月的条码数
                        BigInteger count = recordDao.findByStallNearTime(idsByParent, date);
                        //3.分类进行封装 封装成前端需要的格式
                        result.add(new SaleVO(count, parent.getPoultryParentName()));
                    }
                    resultMap.add(new DateSaleVO(dateUtils.stringToYMD(date), result));
                    //日期自减1
                    date = dateUtils.getDateBeforeDaysDate(1, date);
                    System.out.println(date);
                }
                return resultMap;
            }else { //联系人不为空
                List<Integer> list3 = stallsDao.findIdByUserName(linkName);
                while ((date.after(start) && date.before(end))
                        || date.equals(start) || date.equals(end)) {
                    List<SaleVO> result = new ArrayList<>();
                    for (Poultry_Parent parent : allParent) {
                        String parentId = parent.getId();
                        List<String> idsByParent = poultryCategoryDao.findIdByParent(parentId);
                        //2.查询出这些档口近一个月的条码数
                        BigInteger count = recordDao.findByStallNearTime(list3, idsByParent, date);
                        //3.分类进行封装 封装成前端需要的格式
                        result.add(new SaleVO(count, parent.getPoultryParentName()));
                    }
                    resultMap.add(new DateSaleVO(dateUtils.stringToYMD(date), result));
                    //日期自减1
                    date = dateUtils.getDateBeforeDaysDate(1, date);
                }
                return resultMap;
            }
        }
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    class MarketAndSaleCount {
        String name;
        BigInteger count;
    }
    private void getResult(List<Integer> list, List<Record> all, ArrayList<BarcodeCountVO> vos) {
        for (Integer i :list){
            String stallsName = stallsDao.findById(i).get().getStallsName();
            long count = all.stream().filter(s -> {
                return s.getStallsId().equals(i + ""); //id匹配的
            }).count();
            vos.add(new BarcodeCountVO(stallsName,count));
        }
    }
    private List<Integer> getSandId(List<Integer> list, List<Integer> list2) {
        List<Integer> collect = list.stream().filter(s -> list2.contains(s)).collect(Collectors.toList());
        return collect;
    }
}
